<?php
class Business{
	
	public $name; //商家名
	public $grade; //等级
	public $member_id; //用户ID
	public $site_id;//商家站点 
	public $order_id;//订单id
	public $business_id;//套餐id
	public $sale_id;//销售表id
	
	public function Business($order_id){
	
		$order = M('order')->where(array('id'=>$order_id))->find();
		$this->order_id = $order_id;
		$this->member_id = $order['member_id'];
	}
	
	//是否支付
	public function is_pay(){
		
	   $order = M('order')->where(array('id'=>$this->order_id))->find();
	   if($order['status'] == 1){
	     return false;
	   } 
	   return true;
	}
	
	//判断是否升级
	public function is_updata(){
	   $sale = M('sale') -> where(array('order_id' => $this->order_id)) -> select();
	   $business_siteDB = M('business_site');
	   foreach($sale as $key=>$value){
	     $business_site = $business_siteDB->where(array('product_id'=>$value['product_id'],'site_id'=>__SITEID__))->find();
		 if($business_site){
		   $this->sale_id = $value['id']; 
		   $this->business_id = $business_site['b_id'];
		   M('Sale') -> where(array('id' => $value['id'])) -> save(array('confirm' => 1,'status' => 1));
		   return true;
		 }
	   }
	   return false;
	}
	
	//执行升级
	public function updata(){
		
	   $mes = M('member')->where(array('id'=>$this->member_id))->save(array('is_business'=>1));
       if($mes){
		  $user = M('Member')->where(array('id'=>$this->member_id))->find();
		  sendWXMsg($user['openid'], "亲爱的【".$user['nickname']."】，您的店铺已开通，<a href=\"".U('Member/Vcard/edit',array(),true,false,true)."\">点击进入</a>！");
	      return true;
	   }
	   return false;
	}
	
	//设置商家
	public function setbusiness(){
	    
		if(!$this->is_pay()){
		   return false;
		}
		
		if(!$this->is_updata()){
		   return false;
		}
		
		
		if($this->updata()){
		   $this->affiliate();
		   return true;
		}
		
		return false;
	}
	
	
	//商家等级
	public function getgrade(){
	}
	
	//设置分成金额
	public function setAffiliateconfigMoney(){
	}
	
	
	//分成
	public function affiliate(){
		
	    $business_site = M('business_site')->where(array('b_id'=>$this->business_id))->find();
		//更改销售表,确定分成金额
		$sale = M('sale') -> where(array('id' => $this->sale_id))->save(array('aff_money'=>$business_site['money']));
		import("@.ORG.Affiliate.".$business_site['affiliateconfig']);
		$class = $business_site['affiliateconfig'];
		$affiliate = new $class($this->order_id,$business_site['site_id'],$this->member_id);
		$affiliate->insertlog();
	}
}